load("@rules_license//rules:license.bzl", "license")

package(
    default_applicable_licenses = [":license"],
)

license(
    name = "license",
    package_name = "freeimage",
)

# Dual licensed FreeImage and GPL
# https://freeimage.sourceforge.io/license.html
licenses(["reciprocal"])

exports_files(glob(["license-*.txt"]))

internal_zlib_private_headers = glob(
    include = [
        "Source/ZLib/*.h",
    ],
)

internal_libpng_private_headers = ["Source/LibPNG/png.h"]

internal_openexr_public_headers = glob(
    include = [
        "Source/OpenEXR/*.h",
    ],
)

internal_openexr_half_private_headers = glob(
    include = [
        "Source/OpenEXR/Half/*.h",
    ],
)

internal_openexr_half_sources = glob(
    include = [
        "Source/OpenEXR/Half/*.cpp",
    ],
    exclude = [
        "Source/OpenEXR/Half/toFloat.cpp",
        "Source/OpenEXR/Half/eLut.cpp",
    ],
)

cc_library(
    name = "openexr_half",
    srcs = internal_openexr_half_sources +
           internal_openexr_half_private_headers,
    hdrs = internal_openexr_public_headers,
    copts = [
        "-w",
    ],
    includes = ["Source/OpenEXR/Half"],
)

internal_libjpeg_private_headers = glob(
    include = [
        "Source/LibJPEG/*.h",
    ],
)

internal_libjpeg_sources = glob(
    include = [
        "Source/LibJPEG/*.c",
    ],
    exclude = [
        "Source/LibJPEG/cjpeg.c",
        "Source/LibJPEG/ckconfig.c",
        "Source/LibJPEG/djpeg.c",
        "Source/LibJPEG/example.c",
        "Source/LibJPEG/jpegtran.c",
        "Source/LibJPEG/jmemansi.c",
        "Source/LibJPEG/jmemdos.c",
        "Source/LibJPEG/jmemmac.c",
        "Source/LibJPEG/jmemnobs.c",
        "Source/LibJPEG/rdjpgcom.c",
        "Source/LibJPEG/wrjpgcom.c",
    ],
)

cc_library(
    name = "libjpeg",
    srcs = internal_libjpeg_sources +
           internal_libjpeg_private_headers,
    copts = [
        "-w",
        "-Wno-error=implicit-function-declaration",
    ],
    includes = ["Source/LibJPEG"],
)

internal_openjpeg_private_headers = glob(
    include = [
        "Source/LibOpenJPEG/*.h",
    ],
    exclude = [
        "Source/LibOpenJPEG/*manager.h",
    ],
)

internal_openjpeg_sources = glob(
    include = [
        "Source/LibOpenJPEG/*.c",
    ],
    exclude = [
        "Source/LibOpenJPEG/*manager.c",
        "Source/LibOpenJPEG/t1_generate_luts.c",
    ],
)

cc_library(
    name = "openjpeg",
    srcs = internal_openjpeg_sources +
           internal_openjpeg_private_headers,
    copts = [
        "-w",
    ],
    includes = ["Source/LibOpenJPEG"],
)

internal_tiff_private_headers = glob(
    include = [
        "Source/LibTIFF4/*.h",
    ],
    exclude = [
        "Source/LibTIFF4/tif_win32.h",
        "Source/LibTIFF4/tif_wince.h",
        "Source/LibTIFF4/tif_config.vc.h",
    ],
)

internal_tiff_sources = glob(
    include = [
        "Source/LibTIFF4/*.c",
    ],
    exclude = [
        "Source/LibTIFF4/tif_win32.c",
        "Source/LibTIFF4/tif_wince.c",
        "Source/LibTIFF4/tif_vms.c",
        "Source/LibTIFF4/mkg3states.c",
        "Source/LibTIFF4/mkspans.c",
    ],
)

cc_library(
    name = "tiff",
    srcs = internal_tiff_sources +
           internal_tiff_private_headers +
           internal_libjpeg_private_headers,
    copts = [
        "-w",
        "-Wno-error=implicit-function-declaration",
    ],
    deps = [
        "@zlib",
    ],
    includes = [
        "Source/LibJPEG",
        "Source/LibTIFF4",
    ],
)

freeimage_public_headers = ["Source/FreeImage.h"]

freeimage_sources = glob(
    include = [
        "Source/FreeImage/*.cpp",
        "Source/FreeImageToolkit/*.cpp",
        "Source/Metadata/*.cpp",
    ],
)

freeimage_private_headers = glob(
    include = [
        "Source/*.h",
        "Source/FreeImage/*.h",
        "Source/FreeImageToolkit/*.h",
        "Source/Metadata/*.h",
    ],
    exclude = freeimage_public_headers,
)

cc_library(
    name = "freeimage",
    srcs = freeimage_sources + freeimage_private_headers +
           internal_zlib_private_headers +
           internal_libpng_private_headers,
    hdrs = freeimage_public_headers,
    copts = [
        "-w",
        "-Wno-error=implicit-function-declaration",
    ],
    includes = [
        "Source",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":libjpeg",
        ":openexr_half",
        ":openjpeg",
        ":tiff",
        "@libpng",
        "@libwebp",
        "@openexr//:OpenEXR",
        "@zlib",
    ],
    defines = ["USE_INTERNAL_TIFF_UNIX_ROUTINES",],
)

freeimageplus_sources = glob(
    include = [
        "Wrapper/FreeImagePlus/src/*.cpp",
    ],
)

freeimageplus_public_headers = ["Wrapper/FreeImagePlus/FreeImagePlus.h"]

cc_library(
    name = "freeimageplus",
    srcs = freeimageplus_sources,
    hdrs = freeimageplus_public_headers,
    copts = [
        "-w",
    ],
    includes = [
        "Wrapper/FreeImagePlus",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":freeimage",
    ],
)
